﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using FlowBasisUnitTests.EntityFramework.SampleDomain;

namespace FlowBasisUnitTests.EntityFramework
{
    public class SampleDbContext : DbContext
    {
        public DbSet<Author> Authors { get; set; }
        public DbSet<Book> Books { get; set; }

        public SampleDbContext(string nameOrConnectionString) 
            : base(nameOrConnectionString)
        {            
        }

        public SampleDbContext()
            : base()
        {
            // TODO: Find way to prevent EF from looking for an actual database which seems to slow it down.
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Author>()   
                .HasKey(a => a.AuthorId)
                .ToTable("Author");

            modelBuilder.Entity<Author>()
                .HasMany(a => a.Books)
                .WithOptional(b => b.Author)
                .HasForeignKey(b => b.AuthorId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Book>()
                .HasKey(b => b.BookId)
                .ToTable("Book");
        }
    }
}
